package com.shxy.cloudclassroom.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shxy.cloudclassroom.entity.pojo.ForumCategory;
import com.shxy.cloudclassroom.entity.pojo.ForumPost;
import com.shxy.cloudclassroom.entity.pojo.ForumReply;
import com.shxy.cloudclassroom.entity.pojo.User;
import com.shxy.cloudclassroom.entity.vo.ForumPostDetailVO;
import com.shxy.cloudclassroom.service.ForumCategoryService;
import com.shxy.cloudclassroom.service.ForumPostService;
import com.shxy.cloudclassroom.service.ForumReplyService;
import com.shxy.cloudclassroom.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.shxy.cloudclassroom.utils.SystemConstant.*;

/**
 * @Author 吴汇明
 * @School 绥化学院
 * @CreateTime 2025.10.17
 * 交流互动模块
 */
@Controller
@RequestMapping("/forum")
public class ForumController {

    @Resource
    private ForumPostService forumPostService;

    @Resource
    private ForumCategoryService forumCategoryService;

    /**
     * 论坛首页
     */
    @GetMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size,
                       @RequestParam(required = false) Integer categoryId,
                       Model model) {
        //分页查询
        Page<ForumPost> forumPostPage = new Page<>(page, size);
        LambdaQueryWrapper<ForumPost> wrapper = new LambdaQueryWrapper<>();
        if (categoryId != null && categoryId > 0) {
            wrapper.eq(ForumPost::getCategoryId, categoryId);
        }
        List<ForumPost> forumPostList = forumPostService.getForumPostList(forumPostPage, wrapper);
        List<ForumCategory> categoryList = forumCategoryService.list();
        //置顶帖子
        List<ForumPost> isTop = forumPostService.query().eq("is_top", 1).list();
        //分类
        model.addAttribute(FORUM_LIST, forumPostList);
        model.addAttribute(FORUM_CATEGORY_LIST, categoryList);
        model.addAttribute(IS_TOP_POST, isTop);
        model.addAttribute(HELPER_PAGE, forumPostPage);
        model.addAttribute(SELECTED_FORUM_CATEGORY, categoryId);
        return "/forum/list";
    }

    /**
     * 发布帖子页面
     */
    @GetMapping("/publish")
    public String publishPage(Model model) {
        List<ForumCategory> categoryList = forumCategoryService.list();
        model.addAttribute(FORUM_CATEGORY_LIST,categoryList);
        return "forum/publish";
    }

    /**
     * 发布帖子
     */
    @PostMapping("/publish")
    public String publishPost(ForumPost post, HttpSession session) {
        Map<String, Object> result = forumPostService.publishPost(post, session);
        if (!(boolean) result.get("success")) {
            if (result.containsKey("redirect")) {
                return (String) result.get("redirect");
            }
        }
        return (String) result.getOrDefault("redirect", "redirect:/forum/list");
    }

    /**
     * 帖子详情页面
     */
    @GetMapping("/post/{id}")
    public String postDetail(@PathVariable Integer id,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size,
                             Model model, HttpSession session) {
        ForumPostDetailVO forumPostDetailVO = forumPostService.getPostDetail(id, page, size, session);
        if (forumPostDetailVO == null) {
            return "redirect:/forum/list";
        }
        
        model.addAttribute(HELPER_PAGE, forumPostDetailVO.getReplyPage());
        model.addAttribute(POST_SINGLETON, forumPostDetailVO);
        model.addAttribute(CATEGORY_NAME, forumPostDetailVO.getCategoryName());
        model.addAttribute(POST_SUBMISSION_AUTHOR, forumPostDetailVO.getPostAuthor());
        model.addAttribute(AUTHORS_MAP, forumPostDetailVO.getAuthorMap());
        return "/forum/detail";
    }

    /**
     * 回复帖子
     */
    @PostMapping("/reply")
    public String replyPost(ForumReply reply,
                            @RequestParam Long postId,
                            HttpSession session) {
        Map<String, Object> result = forumPostService.replyPost(reply, postId, session);
        if (!(boolean) result.get("success")) {
            if (result.containsKey("redirect")) {
                return (String) result.get("redirect");
            }
        }
        return (String) result.getOrDefault("redirect", "redirect:/forum/list");
    }
}